


#define _GNU_SOURCE
#include <errno.h>
#include <fcntl.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <time.h>
#include <unistd.h>
#include <xf86drm.h>
#include <xf86drmMode.h>


static void drm_page_flip_handler(int fd, uint32_t frame,
				    uint32_t sec, uint32_t usec,
				    void *data)
{
	static int i = 0;
	uint32_t crtc_id = *(uint32_t *)data;

	if(i==0)
		i=1;
	else
		i=0;
  
	drmModePageFlip(fd, crtc_id, buf[i].fb_id,
			DRM_MODE_PAGE_FLIP_EVENT, data);
}

int main(int argc, char **argv)
{
	int i;
	int size0,size1;
	ev.version = DRM_EVENT_CONTEXT_VERSION;
	ev.page_flip_handler = drm_page_flip_handler;

	drm_init();
	size0 = buf[0].width*buf[0].height;
	size1 = buf[1].width*buf[1].height;
	//buffer1布满红色
	for(i=0;i<size0;i++)
		buf[0].vaddr[i] = RED;
	//buffer2布满蓝色
	for(i=0;i<size1;i++)
		buf[1].vaddr[i] = BLUE;
	
	drmModePageFlip(fd, crtc_id, buf[0].fb_id,
			DRM_MODE_PAGE_FLIP_EVENT, &crtc_id);
	//输入字符
	getchar();
	//切换buffer2
	drmHandleEvent(fd, &ev);
	//输入字符
	getchar();
	//切换buffer1
	drmHandleEvent(fd, &ev);
	//输入字符
	getchar();

	drm_exit();

	exit(0);
}


